package org.chw.callback.rest;

import com.fasterxml.jackson.core.JsonProcessingException;

import org.chw.callback.services.CheckSignatureService;
import org.chw.callback.vo.WechatReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @ClassName: CheckSignatureRest
 * @Description: 微信验证
 * @Author: Vincent.Chen
 * @Create: 2018/6/13 15:09
 * @Version: 1.0
 **/
@RestController
public class CheckSignatureRest {
  @Autowired
  private CheckSignatureService checkSignatureService;

  @Autowired
  private RestTemplate restTemplate;

  /**
   * @Author: Vincent.Chen
   * @Description: 验证消息是否来自微信服务器
   * @Date: 2018/6/12 20:32
   * @Param: [signature, timestamp, nonce]
   * @Return: java.lang.String
   **/
  @RequestMapping(value = "checkSignature", method = RequestMethod.GET,
      produces = MediaType.TEXT_HTML_VALUE)
  public String checkSignature(@RequestParam("signature") String signature,
      @RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce,
      @RequestParam("echostr") String echostr) throws JsonProcessingException {
    if (this.checkSignatureService.checkSignature(signature, timestamp, nonce)) {
      return echostr;
    }
    return "";
  }


  @RequestMapping(value = "check", method = RequestMethod.GET,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public WechatReturn check() {
    // this.restTemplate.getForObject("https://www.baidu.com",
    // String.class);
    WechatReturn wechatReturn = new WechatReturn();
    wechatReturn.setId(1);
    wechatReturn.setName("hongwei");
    return wechatReturn;
  }
}
